package com.sise.bl.controller;

import com.google.gson.Gson;
import com.sise.bl.entity.*;
import com.sise.bl.service.AddressService;
import com.sise.bl.service.CartService;
import com.sise.bl.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/order")
@SessionAttributes({"msg","loginUser"})
public class OrdersController {
    @Autowired
    @Qualifier("ordersService")
    private OrdersService ordersService;

    @Autowired
    @Qualifier("cartService")
    private CartService cartService;

    @Autowired
    @Qualifier("addressService")
    private AddressService addressService;

    @RequestMapping(value = "/preView")
    public ModelAndView preView(@RequestParam("uid") String uid, ModelAndView modelAndView) {
        //调用业务逻辑 （地址，购物车）
        List<Address> addressList = addressService.findAddressByUid(Integer.valueOf(uid));

        List<Cart> cartList = cartService.findAll(Integer.valueOf(uid));
        //放入共享域即可
        modelAndView.addObject("addressList", addressList);
        modelAndView.addObject("cartList", cartList);
        modelAndView.setViewName("order");

        return modelAndView;
    }

    @RequestMapping(value = "/create")
    public String create(@RequestParam("aid") String aid, @RequestParam("uid") String uid,
                         @RequestParam("sum") String sum) {
        //调用业务逻辑生成订单
        ordersService.createOrder(aid, uid, sum);

        //转发到订单展示的方法
        return "redirect:/order/show";
    }

    @RequestMapping(value = "/show")
    public ModelAndView show(ModelAndView modelAndView, Model model) {
        User user = (User) model.getAttribute("loginUser");

        if (user == null) {
            modelAndView.addObject("msg", "登录后可以查看订单！");
            modelAndView.setViewName("login");
            return modelAndView;
        }

        List<Orders> ordersList = ordersService.findOrdersByUid(user.getUid());
        modelAndView.addObject("ordersList", ordersList);
        modelAndView.setViewName("orderList");
        return modelAndView;
    }

    @RequestMapping(value = "/detail")
    public ModelAndView detail(@RequestParam("oid") String oid,ModelAndView modelAndView) {

        Orders order = ordersService.findOrderByOid(oid);
        modelAndView.addObject("order", order);
        modelAndView.setViewName("orderDetail");

        return modelAndView;
    }


    @RequestMapping(value = "/success")
    public ModelAndView success(@RequestParam("oid")String oid,@RequestParam("result") String result,ModelAndView modelAndView) {
        Gson gson = new Gson();
        WeiXin weiXin = gson.fromJson(result, WeiXin.class);

        String result_code = weiXin.getResult().getResult_code();

        if (result_code != null && result_code.equals("SUCCESS"))
        {
            //支付成功
            //修改状态
            //订单列表页面
            ordersService.updateStateByOid(oid);
            modelAndView.addObject("msg", "订单："+oid+" 支付成功！");
            modelAndView.setViewName("message");

            return modelAndView;
        }else{
            //支付失败
            modelAndView.addObject("msg", "订单："+oid+" 支付失败！");
            //message.jsp页面
            modelAndView.setViewName("message");
            return modelAndView;
        }
    }
}
